package com.bdth.weget.home.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.home.entity.Rule;
import com.bdth.weget.home.mapper.RuleMapper;
import com.bdth.weget.home.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LGP
 * @since 2018-11-13
 */
@Service
public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements RuleService {
    @Autowired
    private RuleMapper ruleMapper;

    @Override
    public Page<Rule> listPages(Pagination pagination, String name, String type, String title) {
        EntityWrapper<Rule> entityWrapper = new EntityWrapper<>();
        if(title!=null && !title.trim().equals(""))
            entityWrapper.like("title",title);
        if(type!=null)
            entityWrapper.eq("type",type);
        if(name!=null && !name.trim().equals(""))
            entityWrapper.like("name",name);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Rule> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<Rule> items = ruleMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }

    public Integer listTotal(String name, String type, String title) {
        EntityWrapper<Rule> entityWrapper = new EntityWrapper<>();
        if(title!=null && !title.trim().equals(""))
            entityWrapper.like("title",title);
        if(type!=null)
            entityWrapper.eq("type",type);
        if(name!=null && !name.trim().equals(""))
            entityWrapper.like("name",name);
        Integer counts = ruleMapper.selectCount(entityWrapper);
        return counts;
    }

    @Override
    public boolean update(Rule bean) {
        if(bean.getId()>0) {
            bean.setUptime(new Date());
            Integer result = ruleMapper.updateById(bean);
            if(result>0)
                return true;
        }
        return false;
    }

    @Override
    public boolean save(Rule bean) {
        bean.setCrtime(new Date());
        Integer result = ruleMapper.insert(bean);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        Integer result = ruleMapper.deleteById(id);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean remove(String ids) {
        String[] id = ids.split(",");
        Integer result = 0;
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            Integer r = ruleMapper.deleteById(intId);
            if(r>0)
                result++;
        }
        if(result>0)
            return true;
        return false;
    }

    @Override
    public List<Rule> listAll() {
        EntityWrapper<Rule> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        return ruleMapper.selectList(entityWrapper);
    }
}
